package com.wtcrazy.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtcrazy.dao.AdminMapper;
import com.wtcrazy.entity.*;
import com.wtcrazy.service.IAdminService;
import com.wtcrazy.utils.JsonResult;
import com.wtcrazy.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserServiceImpl userService;

    @Override
    public JsonResult login(Admin admin) {
        Admin dbAdmin = adminMapper.login(admin);
        if (dbAdmin != null) {
            String token = JwtUtil.createToken(dbAdmin.getId());
            dbAdmin.setToken(token);
            return JsonResult.builder().result(dbAdmin).code("20000").message("登录成功").build();
        }
        return JsonResult.builder().result(null).message("登录异常").code("50000").build();
    }

    @Override
    public JsonResult checkToken(String token) {
        return userService.checkToken(token);
    }

    @Override//重写方法
    public List<User> getAllUsers() {
        return adminMapper.getAllUsers();
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, String username) {
        pageNum = (pageNum - 1) * pageSize;
        username = "%" + username + "%";
        List<User> data = adminMapper.selectUserPage(pageNum, pageSize, username);
        Integer total = adminMapper.selectUserTotal(username);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @Override
    public Map<String, Object> findPost(Integer pageNum,
                                        Integer pageSize,
                                        String title,
                                        String content) {
        pageNum = (pageNum - 1) * pageSize;
        List<Post> data = adminMapper.selectPostPage(pageNum, pageSize, title, content);
        Integer total = adminMapper.selectPostTotal(title, content);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }


    @Override
    public String deleteUser(String id) {//@GetMapping不支持@RequestBody
        adminMapper.deleteUser(id);//先删除点赞表,再删除帖子表,最后删除用户表
        return "删除成功";
    }

    @Override
    public List<Object> getView() {
        return adminMapper.getView();
    }

    @Override
    public List<Comment> getcomment() {
        return adminMapper.getcomment();
    }

    @Override
    public List<Record> getRecord() {
        return adminMapper.getRecord();
    }

    @Override
    public Map<String, Object> findComment(Integer pageNum,
                                           Integer pageSize,
                                           String content) {
        pageNum = (pageNum - 1) * pageSize;
        List<Comment> data = adminMapper.selectCommentPage(pageNum, pageSize, content);
        Integer total = adminMapper.selectCommentTotal(content);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @Override
    public Map<String, Object> findOrder(Integer pageNum,
                                         Integer pageSize,
                                         String id) {
        pageNum = (pageNum - 1) * pageSize;
        List<Order> data = adminMapper.selectOrderPage(id, pageNum, pageSize);
        Integer total = adminMapper.selectOrderTotal(id);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
